package com.baizhi.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.baizhi.entity.Cart;
import com.baizhi.entity.Product;
import com.baizhi.service.BookService;
import com.baizhi.service.impl.BookServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 王雪洋
 * @Time 2017年5月3日 下午10:01:14
 * @disc:购物车实体类
 */
public class CartAction extends ActionSupport {
	private Cart cart = new Cart();
	private Product p = new Product();
	private Integer pid;
	private Map cmap;
	private HttpSession session = ServletActionContext.getRequest().getSession();
	private BookService bs = new BookServiceImpl();
	private Integer num;
	private Double totalprice = 0.0;
	private Double savePrice = 0.0;
	private Integer removeCount=0;
	/*
	 * 添加购物车
	 */
	public String addCart() {
		cmap = (Map) session.getAttribute("cmap");
		if (cmap == null) {
			// 初始状态存放map
			cmap = new HashMap();
		}
		//判断商品是否已经添加如果已添加只是更新数量
		if(cmap.containsKey(pid)){
			cart = (Cart) cmap.get(pid);
			cart.setPnum(cart.getPnum()+1);
		}else{
			cart.setPflag(1);
			cart.setPnum(1);
		}
		Product product = bs.findBookById(pid);
		// 计算总价格
		cart.setTotalprice(product.getP_dangprice());
		// 计算总节省价格
		cart.setSavetotalprice(product.getP_marketprice() - cart.getTotalprice());
		// 将购物信息放入购物车map
		cart.setProduct(product);
		cmap.put(pid, cart);
		session.setAttribute("cmap", cmap);
		totalprice = getTotalprice();
		savePrice = getSavePrice();
		return SUCCESS;
	}

	/*
	 * 删除或恢复购物车
	 */
	public String deleteCart() {
		cmap = (Map) session.getAttribute("cmap");
		Cart c = (Cart) cmap.get(pid);
		// 拿到要删除的商品修改状态
		if (c.getPflag() == 1) {
			c.setPflag(2);
		} else {
			c.setPflag(1);
		}
		/* cmap.put(c.getProduct().getCategoryid(),c); */
		totalprice = getTotalprice();
		savePrice = getSavePrice();
		return SUCCESS;
	}

	/*
	 * 更新购物车中的商品数量
	 */
	public String updateNum() {
		if(num != null){
			cmap = (Map) session.getAttribute("cmap");
			cart = (Cart) cmap.get(pid);
			cart.setPnum(num);
			Product product = cart.getProduct();
			// 计算总价格
			cart.setTotalprice(product.getP_dangprice() * num);
			cart.setSavetotalprice(product.getP_marketprice() * num - cart.getTotalprice());
		}
		totalprice = getTotalprice();
		savePrice = getSavePrice();
		return SUCCESS;
	}

	/*
	 * 遍历购物车
	 */
	public String findCarts() {
		cmap = (Map) session.getAttribute("cmap");
		totalprice = getTotalprice();
		savePrice = getSavePrice();
		return SUCCESS;
	}

	public Cart getCart() {
		return cart;
	}

	public void setCart(Cart cart) {
		this.cart = cart;
	}

	public Map getCmap() {
		return cmap;
	}

	public void setCmap(Map cmap) {
		this.cmap = cmap;
	}

	public Integer getNum() {
		return num;
	}

	public void setNum(Integer num) {
		this.num = num;
	}

	public Product getP() {
		return p;
	}

	public void setP(Product p) {
		this.p = p;
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public Double getTotalprice() {
		cmap = (Map) session.getAttribute("cmap");
		double sum = 0;
		if (cmap != null) {
			for (Object i : cmap.keySet()) {
				Cart c = (Cart) cmap.get(i);
				if (c.getPflag() == 1)
					sum += c.getTotalprice();
				else if(c.getPflag() == 2)
					removeCount++;
			}
		}
		return sum;
	}

	public Double getSavePrice() {
		cmap = (Map) session.getAttribute("cmap");
		double sum = 0;
		if (cmap != null) {
			for (Object i : cmap.keySet()) {
				Cart c = (Cart) cmap.get(i);
				if (c.getPflag() == 1)
					sum += c.getSavetotalprice();
			}
		}
		return sum;
	}

	public void setTotalprice(Double totalprice) {
		this.totalprice = totalprice;
	}

	public void setSavePrice(Double savePrice) {
		this.savePrice = savePrice;
	}

	public Integer getRemoveCount() {
		return removeCount;
	}
	public void setRemoveCount(Integer removeCount) {
		this.removeCount = removeCount;
	}
}
